package com.baidu.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.mapper.RoleMapper;
import com.baidu.mapper.UserMapper;
import com.baidu.mapper.PermissionMapper;
import com.baidu.pojo.Permission;
import com.baidu.pojo.Role;
import com.baidu.pojo.User;
import com.baidu.service.UserService;
import com.baidu.utils.MD5Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;


@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;


    /**
     * 用户对应有->角色->权限
     *
     * @param s
     * @return
     */
    @Override
    public User findByUsername(String s) {

        User user = userMapper.findByUsername(s);

        if (user == null) {
            return null;
        }

        Integer userId = user.getId();


        Set<Role> roles = roleMapper.findRoleByUid(userId);
        if (roles != null) {
            user.setRoles(roles);
            for (Role role : roles) {

                Integer roleId = role.getId();

                Set<Permission> permissionSet = permissionMapper.findPermissionByRid(roleId);
                if (permissionSet != null) {
                    role.setPermissions(permissionSet);

                }

            }


        }


        return user;
    }

    /**
     * 分页和条件查询用户信息
     *
     * @param queryPageBean
     * @return
     */

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();//当前页码
        Integer pageSize = queryPageBean.getPageSize();//每页显示最大条数
        String condition = queryPageBean.getQueryString();//传来的条件
        PageHelper.startPage(currentPage, pageSize);//会在sql加上limit
        Page<User> page = userMapper.findPageByCondition(condition);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据表单信息添加用户
     *
     * @param user
     */

    @Override
    public void save(User user) throws Exception {
        String password = "123456";
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        user.setPassword(encode);


        userMapper.add(user);
    }

    /**
     * 查询所有角色，展示在前端页面
     */

    @Override
    public List<Role> showAllRole() {
        return roleMapper.findAll();
    }

    /**
     *通过用户ID查询他所拥有的角色
     * @param id
     * @return
     */

    @Override
    public List<Integer> showRoleById(Integer id) {
        return userMapper.findById(id);
    }

    /**
     * 通过用户id和所选择的角色，修改当前用户所拥有的角色
     * @param rids
     * @param user
     */

    @Override
    public void edit(Integer[] rids,User user) {
        //1.先查询当前用户是否具有角色
        Integer id = user.getId();
        Long count = userMapper.findRoleUser(id);
        if(count == 0 ){//说明当前用户没有角色，则直接进行添加
            for (Integer rid : rids) {
                userMapper.addRole(id,rid);
            }

        }else {//说明当前用户有角色，先删除当前用户用户角色中间关系表,再进行添加
            userMapper.deleteUserAndRole(id);
            for (Integer rid : rids) {
                userMapper.addRole(id,rid);
            }
        }

    }

    /**
     *通过用户id删除指定用户,先删除中间表，再删除用户表
     */

    @Override
    public void delete(Integer id) {
        userMapper.deleteUserAndRole(id);//删除中间表指定id
        userMapper.deleteUser(id);//删除用户表指定id
    }



    /**
     *通过用户id查找当前用户的信息
     * @param id
     *
     */

    @Override
    public User showUserById(Integer id) {
        return userMapper.findUserById(id);
    }

}
